<template>
	<view class="comfirm-order back-color ">
		<Lines></Lines>
		<view class="order-path-info" @click="goPath">
			<template v-if="path">
				<view class="info-title">
					<view class="title-name">收货人：{{path.userName}}</view>
					<view>{{path.tel}}</view>
				</view>
				<view class="order-address">
					<view>收货地址：{{path.province+path.city+path.district}} {{path.details}}</view>
				</view>
			</template>
			<template v-else>
				<view class="no-path-text">请选择地址</view>
			</template>
		</view>
		<view class="goods-list">
			<view class="orderNumber_text">
				订单号：{{orderNumber}}
			</view>
			<view class="goods-content" v-for="(item,index) in goodsList" :key='index'>
				<image class="goods-image" :src="item.imgUrl" mode=""></image>
				<view class="goods-text">
					<view class="goods-name">{{item.name}}</view>
					<view style="font-size: 26rpx;" class="f-color">颜色：黑色</view>
					<view class="f-active-color" style="font-size: 24rpx;">七天无理由</view>
				</view>
				<view>
					<view>￥{{item.pprice}}</view>
					<view style="text-align: right;" class="f-color">*{{item.num}}</view>
				</view>
			</view>
		</view>
		<!-- 底部提交订单 -->
		<view class="order-footer">
			<view class="footer-text">合计：<text class="f-active-color" style="font-weight: bold;	"><sub>￥</sub>{{totalCount.pprice}}</text></view>
			<view class="footer-btn" @click="goPayment()">提交订单</view>
		</view>
	</view>
</template>

<script>
	import $http from '@/common/api/request.js'
	import {mapGetters,mapState,mapMutations} from 'vuex'
	import Lines from '@/components/common/Line.vue'
	export default {
		data() {
			return {
				path:false,
				item:{},
			}
		},
		components: {
			Lines
		},
		computed:{
			...mapState({
				cartList:state=>state.cart.cartList,
				orderNumber:state=>state.order.orderNumber,
				selectedList:state=>state.cart.selectedList
			}),
			...mapGetters(['defaultPath','totalCount']),
			// 根据商品列表找到对应的e.detail的id，最终返回数组
			goodsList(){
				return this.item.map(id=>{
					return this.cartList.find(v=>v.id==id);
				})
			}
		},
		onLoad(e) {
			// this.pathId=e.id
			// 选中的商品id集合
			this.item=JSON.parse(e.detail)
			console.log(JSON.parse(e.detail))
			// 如果有默认地址则把path变为默认地址
			$http.request({
				url: '/selectAddress',
				method:"post",
				header:{
					'content-type': 'application/x-www-form-urlencoded',
					token:true
				},
			}).then((res) =>{
				this.__initAddress(res)
				if(this.defaultPath.length>0){
					this.path=this.defaultPath[0]
				}
			}).catch(() => {
				uni.showToast({
					title: '请求失败',
					icon: 'none'
				})
			})
			
			
			// $on自定义事件触发--修改默认地址
			uni.$on('selectDefaultPath',(item)=>{
				this.path=item
			})
		},
		onUnload() {
			uni.$off('selectDefaultPath')
		},
		methods: {
			...mapMutations(['__initAddress']),
			// 跳转到地址选择
			goPath(){
				uni.navigateTo({
					url:'../my-path-list/my-path-list?type=selectedpath'
				})
			},
			// 跳转到订单支付页
			goPayment(){
				const {userName,tel,province,city,district,details}=this.path
				if(!this.path){
					return uni.showToast({
						title:'请选择收货地址',
						icon:'none'
					})
				}
				// console.log(JSON.stringify(this.selectedList))
				$http.request({
					url: '/submitOrder',
					method:"post",
					header:{
						'content-type': 'application/x-www-form-urlencoded',
						token:true
					},
					data:{
						orderId:this.orderNumber,
						shopArr:this.selectedList,
						userName,
						tel,
						address:province+'-'+city+'-'+district+'-'+details
					}
				}).then((res) =>{
					if(res.success){
						uni.navigateTo({
							url:'../payment/payment?details='+JSON.stringify({
								totalPrice:this.totalCount.pprice,
								orderNumber:this.orderNumber
							})
						})
					}
				}).catch(() => {
					uni.showToast({
						title: '请求失败',
						icon: 'none'
					})
				})
				
				
				// 创建一个订单
				// console.log(1)
				// uni.navigateTo({
				// 	url:'../payment/payment?details='+JSON.stringify({
				// 		totalPrice:this.totalCount.pprice
				// 	})
				// })
				
			}
		}
	}
</script>

<style scoped>
	.comfirm-order {
		position: absolute;
		left: 0;
		top: 0;
		bottom: 0;
		right: 0;
		width: 100%;
		height: 100%;
	}

	.order-path-info {
		padding: 20rpx;
		background-color: #FFFFFF;
		line-height: 40rpx;
	}

	.info-title {
		display: flex;
		justify-content: space-between;
		padding-bottom: 20rpx;
	}

	.title-name {
		font-weight: bold;
	}
	.goods-list{
		padding: 20rpx 0;
		margin-top: 20rpx;
		background-color: #FFFFFF;
	}
	.goods-content {
		background-color: #F7f7f7;
		padding: 10rpx 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.goods-image {
		width: 160rpx;
		height: 160rpx;
	}
	
	.goods-text {
		width: 360rpx;
		padding: 0 10rpx;
	}
	
	.goods-name {
		font-size: 28rpx;
		padding: 10rpx 0;
		height: 66rpx;
		overflow: hidden;
	}
	.order-footer{
		background-color: #FFFFFF;
		position: fixed;
		width: 100%;
		left: 0;
		bottom: 0;
		display: flex;
		justify-content: flex-end;
		height: 200rpx;
	}
	.footer-text{
		line-height: 100rpx;
		padding-right: 20rpx;
	}
	.footer-btn{
		width: 200rpx;
		height: 100rpx;
		background-color: #00B7FF;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #FFFFFF;
	}
	.no-path-text{
		line-height: 80rpx;
		font-weight: bold;
	}
	.orderNumber_text{
		padding: 20rpx;
		font-size: 24rpx;
		color: #A1A1A1
	}
</style>
